package com.fx.educenter.controller;


import com.fx.commonuntils.JwtUtils;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.commonuntils.commonresponse.UcenterMemberOrder;
import com.fx.educenter.entity.UcenterMember;
import com.fx.educenter.entity.vo.RegisterVo;
import com.fx.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-14
 */
@Api(description = "登录管理系统")
@RestController
@RequestMapping("/educenter/ucentermember")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin//解决跨域问题
public class UcenterMemberController {


    final private UcenterMemberService memberService;

    @PostMapping("/login")
    public ReturnMessage<Object> loginUser(@RequestBody UcenterMember member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成

        return memberService.login(member);

    }


    //注册
    @PostMapping("/register")
    public ReturnMessage<Object> registerUser(@RequestBody RegisterVo registerVo) {

        return memberService.register(registerVo);


    }

    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public ReturnMessage<Object> getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return ReturnMessageUtils.success(member);
    }




    //根据用户id获取用户信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //查询某一天注册人数
    @GetMapping("countRegister/{day}")
    public ReturnMessage<Object> countRegister(@PathVariable String day) {

        return memberService.countRegisterDay(day);
    }






}

